package com.maze.activity.service.impl;

import java.util.List;
import com.maze.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.maze.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.maze.activity.domain.Registrations;
import com.maze.activity.mapper.ActivitiesMapper;
import com.maze.activity.domain.Activities;
import com.maze.activity.service.IActivitiesService;

import javax.annotation.Resource;

/**
 * 活动发布Service业务层处理
 *
 * @author xixi.wang
 * @date 2025-03-30
 */
@Service
public class ActivitiesServiceImpl implements IActivitiesService
{
    @Resource
    private ActivitiesMapper activitiesMapper;

    /**
     * 查询活动发布
     *
     * @param actId 活动发布主键
     * @return 活动发布
     */
    @Override
    public Activities selectActivitiesByActId(Long actId)
    {
        return activitiesMapper.selectActivitiesByActId(actId);
    }

    /**
     * 查询活动发布列表
     *
     * @param activities 活动发布
     * @return 活动发布
     */
    @Override
    public List<Activities> selectActivitiesList(Activities activities)
    {
        return activitiesMapper.selectActivitiesList(activities);
    }

    /**
     * 新增活动发布
     *
     * @param activities 活动发布
     * @return 结果
     */
    @Transactional
    @Override
    public int insertActivities(Activities activities)
    {
        activities.setCreateTime(DateUtils.getNowDate());
        int rows = activitiesMapper.insertActivities(activities);
        insertRegistrations(activities);
        return rows;
    }

    /**
     * 修改活动发布
     *
     * @param activities 活动发布
     * @return 结果
     */
    @Transactional
    @Override
    public int updateActivities(Activities activities)
    {
        activities.setUpdateTime(DateUtils.getNowDate());
        activitiesMapper.deleteRegistrationsByActivityId(activities.getActId());
        insertRegistrations(activities);
        return activitiesMapper.updateActivities(activities);
    }

    /**
     * 批量删除活动发布
     *
     * @param actIds 需要删除的活动发布主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivitiesByActIds(Long[] actIds)
    {
        activitiesMapper.deleteRegistrationsByActivityIds(actIds);
        return activitiesMapper.deleteActivitiesByActIds(actIds);
    }

    /**
     * 删除活动发布信息
     *
     * @param actId 活动发布主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivitiesByActId(Long actId)
    {
        activitiesMapper.deleteRegistrationsByActivityId(actId);
        return activitiesMapper.deleteActivitiesByActId(actId);
    }

    /**
     * 新增报名记录信息
     *
     * @param activities 活动发布对象
     */
    public void insertRegistrations(Activities activities)
    {
        List<Registrations> registrationsList = activities.getRegistrationsList();
        Long actId = activities.getActId();
        if (StringUtils.isNotNull(registrationsList))
        {
            List<Registrations> list = new ArrayList<Registrations>();
            for (Registrations registrations : registrationsList)
            {
                registrations.setActivityId(actId);
                list.add(registrations);
            }
            if (list.size() > 0)
            {
                activitiesMapper.batchRegistrations(list);
            }
        }
    }
}
